//logs.js
var util = require('../../utils/util.js')
Page({
  data: {
    CompanyName:'',
    provinceid:1946,
    cityid:1990,
    cityindex:5,
    provinceindex:18,
    provincelist:[],
    citylist:[],
    provinceidarr:[],
    cityidarr:[],
    
    countyindex:2,
    countylist:[],
    countyidarr:[],
    countyid:1993,

    companylightness:'',
    summary:'',
    hiddenModal:true,
    phone:'',
    src:'https://lendgiant.cn/plat/images/pic.png',
    msg:'',
    sessionKey:'',
    openid:'',
    imgurl:''
  },
  onLoad: function () {
   var that =this;    
 
  //获取地址省份 
     wx.request({
      url:'https://lendgiant.cn/index/getAreas', 
      data: {      
      },
     header: {'Content-Type': 'application/json'},
      success: function(res) {     
        that.data.provincelist=[];
        that.data.provinceidarr=[];
      for( var i=0; i<res.data.list.length;i++){
          that.data.provincelist.push( res.data.list[i].name)   
          that.data.provinceidarr.push(res.data.list[i].id); 
      }        
       that.setData({
           provincelist: that.data.provincelist,
           provinceidarr: that.data.provinceidarr
       })       
      }
    })

  //获取城市
     wx.request({
      url:'https://lendgiant.cn/index/getAreas', 
      data: { 
         'parentid': 1946  
      },
       header: {'Content-Type': 'application/json'},
      success: function(res) {      
        that.data.citylist=[];
        that.data.cityidarr=[];        
      for( var i=0; i<res.data.list.length;i++){
          that.data.citylist.push( res.data.list[i].name);
          that.data.cityidarr.push(res.data.list[i].id)   
      }        
       that.setData({
           citylist: that.data.citylist,
           cityid:that.data.cityidarr[0]
       });
      
      }
    })

     //获取城镇 区
     wx.request({
      url:'https://lendgiant.cn/index/getAreas', 
      data: { 
         'parentid':  that.data.cityid  
      },
       header: {'Content-Type': 'application/json'},
      success: function(res) {
        that.data.countylist=[];
        that.data.countyidarr=[];        
      for( var i=0; i<res.data.list.length;i++){
          that.data.countylist.push( res.data.list[i].name);
          that.data.countyidarr.push(res.data.list[i].id)   
      }        
       that.setData({
           countylist: that.data.countylist
       });
      
      }
    })
  },

   bindPickerChangeProvince:function(e){
   //  console.log('picker省份更改，携带值为', e.detail.value)
    this.setData({
      provinceindex:e.detail.value, 
      provinceid:this.data.provinceidarr[e.detail.value],
    });  
    var that=this;
   wx.request({
      url:'https://lendgiant.cn/index/getAreas', 
      data: { 
         'parentid': that.data.provinceid   
      },
       header: {'Content-Type': 'application/json'},
      success: function(res) {      
        that.data.citylist=[];
        that.data.cityidarr=[];     
      for( var i=0; i<res.data.list.length;i++){
          that.data.citylist.push( res.data.list[i].name);
          that.data.cityidarr.push(res.data.list[i].id)   
      }        
       that.setData({
           citylist: that.data.citylist
       });      
      }
    })

  },
  bindPickerChangeCity:function(e){
  //console.log('picker城市更改，携带值为', e.detail.value)
    this.setData({
      cityindex: e.detail.value,   
      cityid:this.data.cityidarr[e.detail.value]   
    });
  var that=this;
   wx.request({
      url:'https://lendgiant.cn/index/getAreas', 
      data: { 
         'parentid': that.data.cityid   
      },
       header: {'Content-Type': 'application/json'},
      success: function(res) {   
           console.log(that.data.cityid);
        that.data.countylist=[];
        that.data.countyidarr=[];     
      for( var i=0; i<res.data.list.length;i++){
          that.data.countylist.push( res.data.list[i].name);
          that.data.countyidarr.push(res.data.list[i].id)   
      }        
       that.setData({
           countylist: that.data.countylist
       });      
      }
    })
  },
 
  bindPickerChangeCounty:function(e){
    //console.log('picker镇 区 更改，携带值为', e.detail.value)
      this.setData({
        countyindex: e.detail.value,   
        countyid:this.data.countyidarr[e.detail.value]   
      });
  },
   listernerCompanyNameChange:function(e){
     this.data.CompanyName=e.detail.value
  },
  listernerPhoneChange:function(e){
    this.data.phone=e.detail.value
  },
  listernerAddressChange:function(e){
    this.data.address=e.detail.value
  },
  charChangeSummary:function(e){
    this.setData({
      summary:e.detail.value
    })
    
  },
  charChangeCompanylightness:function(e){
    this.setData({
      companylightness:e.detail.value
    })
    
  },
   //保存公司
  saveCompany:function(e){   
    var that=this;
    if( that.data.CompanyName==undefined || that.data.CompanyName.length==0){
        that.setData({
              msg:'请填写公司名称！',
              hiddenModal:!that.data.hiddenModal,
    });         
      return false;
     }

      if( that.data.address==undefined || that.data.address.length==0){
        that.setData({
              msg:'请填写详细地址！',
              hiddenModal:!that.data.hiddenModal,
    });         
      return false;
     }
      if( that.data.summary==undefined || that.data.summary.length==0){
        that.setData({
              msg:'请填写公司简介！',
              hiddenModal:!that.data.hiddenModal,
    });         
      return false;
     }
       if( that.data.companylightness==undefined || that.data.companylightness.length==0){

        that.setData({
              msg:'请填写公司福利！',             
              hiddenModal:!that.data.hiddenModal,
    });   
    
      return false;
     }
var r = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0678]|18[0-9]|14[57])[0-9]{8}$/;
      if( that.data.phone==undefined || that.data.phone.length==0  ){
        //|| !r.test(that.data.phone)
        that.setData({
              msg:'请填写正确的公司联系电话！',
              hiddenModal:!that.data.hiddenModal,
    });         
      return false;
     }
console.log(that.data.CompanyName +" "+
       +" "+that.data.provinceid,
       +" "+that.data.cityid,
        +" "+that.data.countyid || 0,
        +" "+that.data.address,
        +" "+that.data.phone,
       +" "+that.data.summary,
      +" "+that.data.companylightness+"  "+
      that.data.imgurl);
      wx.request({
       url:'http://lendgiant.cn/explabor/saveCompanyWXX',
      data: { 
        'companyname':that.data.CompanyName,
        'provinceid':that.data.provinceid,
        'cityid':that.data.cityid,
        'countyid':that.data.countyid || 0,
        'address':that.data.address,
        'phone':that.data.phone,
        'summary':that.data.summary,
        'companylightness':that.data.companylightness,
        'imgurl':that.data.imgurl,
        'session_token':wx.getStorageSync('session_token')
      },
    method: 'POST',
    header: {'content-type': 'application/x-www-form-urlencoded'},
      success: function(res) {
      // if(!res.data.result){
         that.setData({
           hiddenModal:!that.data.hiddenModal,
           msg:res.data.msg
         })      
      // }
      console.log(that.data.msg);
     }
      })

  },
   listenerConfirm:function(){
      this.setData({
           hiddenModal:!this.data.hiddenModal
         })
  },
  listenerCancel:function(){
      this.setData({
           hiddenModal:!this.data.hiddenModal
         })
  },

uploadPhoto:function() {
    var that = this; 
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;
        upload(that, tempFilePaths);
      }
    })
  }



})
function upload(page, path) {
  var that=this;
  wx.showToast({
    icon: "loading",
    title: "正在上传"
  }),
    wx.uploadFile({
      url:'https://lendgiant.cn/common' + "/imageUpload_wxxiaochengxu?tablename=company&colname=shopimage1url&uploadfile=uploadfile&session_token="+wx.getStorageSync('session_token'),
      filePath: path[0], 
      name: 'uploadfile',
      header: { "Content-Type": "multipart/form-data" },
      formData: {
        //和服务器约定的token, 一般也可以放在header中
        'session_token': wx.getStorageSync('session_token')
      },
      success: function (res) {
        console.log(res.statusCode);
        console.log(res);
        console.log(JSON.parse(res.data).src);
        console.log(JSON.parse(res.data).succ);
      
        // that.data.imgurl=JSON.parse(res.data).src
        
        if (res.statusCode != 200) { 
          wx.showModal({
            title: '提示',
            content: '上传成功',
            showCancel: false
          })
         
        }
        var data = res.data
        page.setData({  //上传成功修改显示头像
          src: path[0]
        })
       
      },
      fail: function (e) {
        console.log(e);
        wx.showModal({
          title: '提示',
          content: '上传失败',
          showCancel: false
        })
      },
      complete: function () {
        wx.hideToast();  //隐藏Toast

      }
    })
}